import axios from "axios";
import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
// 登录验证
export default new Vuex.Store({
  state: {
    user: false,
    //班级对应的id
    c_id: JSON.parse(sessionStorage.getItem("c_id") || 0),
    //用户id
    userid: JSON.parse(sessionStorage.getItem("userid") || 0),
    //用户级
    level: JSON.parse(sessionStorage.getItem("level") || 0),
    //用户账号
    useraccount: JSON.parse(sessionStorage.getItem("useraccount") || "0"),
    //角色类型
    type_id: JSON.parse(sessionStorage.getItem("type_id") || 0),
    //侧边栏、权限数据
    rightList: JSON.parse(sessionStorage.getItem("rightList") || "[]"),
    //作业或者考试对应的主键id
    homeortestid: JSON.parse(sessionStorage.getItem("homeortestid") || 0),
    //文章
    article: JSON.parse(sessionStorage.getItem("article") || "{}"),
    //学生
    student: JSON.parse(sessionStorage.getItem("student") || "{}"),

    //完成作业表的id
    conpletedid: JSON.parse(sessionStorage.getItem("conpletedid") || 0),
    //作业或者考试对应的主键id
    article: JSON.parse(sessionStorage.getItem("article") || "{}")
  },
  mutations: {
    // 登录
    login(state, user) {
      state.user = user;
      localStorage.setItem("userInfo", user);
      console.log(user);
    },
    // 退出
    logout(state, user) {
      state.user = "";
      localStorage.setItem("userInfo", "");
    },
    //角色下路由集合
    setRightList(state, data) {
      state.rightList = data;
      //存到sessionStorage中避免页面刷新数据丢失
      sessionStorage.setItem("rightList", JSON.stringify(data));
    },
    //用户id
    setUserid(state, userid) {
      state.userid = userid;
      sessionStorage.setItem("userid", userid);
    },
    //用户级
    setLevel(state, level) {
      state.level = level;
      sessionStorage.setItem("level", level);
    },
    // 角色类型
    setType_id(state, type_id) {
      state.type_id = type_id;
      sessionStorage.setItem("type_id", JSON.stringify(type_id));
    },
    // 用户账号
    setAccount(state, useraccount) {
      state.useraccount = useraccount;
      console.log(typeof useraccount);
      sessionStorage.setItem("useraccount", JSON.stringify(useraccount));
    },
    // 班级id
    setClassid(state, c_id) {
      state.c_id = c_id;
      sessionStorage.setItem("c_id", JSON.stringify(c_id));
    },
    // 用户作业或者考试的id
    setHomeortestid(state, homeortestid) {
      state.homeortestid = homeortestid;
      sessionStorage.setItem("homeortestid", JSON.stringify(homeortestid));
    },

    //将文章存到这里面

     // 完成作业的id
     setConpletedid(state, conpletedid) {
      state.conpletedid = conpletedid;
      sessionStorage.setItem("conpletedid", JSON.stringify(conpletedid));
     },
    // 用户作业或者考试的id
    setArticle(state, article) {
      state.article = article;
      sessionStorage.setItem("article", JSON.stringify(article));
    },
    //存储学生数据
    setStudent(state, student) {
      state.student = student;
      sessionStorage.setItem("student", JSON.stringify(student));
    }
  },
  actions: {},
  getters: {}
});
